<?php

namespace App\Http\Controllers\Admin;

use App\Models\HousingResource;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HousingResourceController extends Controller
{
    //房源列表
    public function housingIndex(Request $request){
        $where = function ($query) use ($request) {
            //区块名称
            if ($request->has('block_name') and $request->block_name != '') {
                $search = "%" . $request->block_name . "%";
                $query->where('block_name', 'like', $search);
            }
            //小区名称
            if ($request->has('housing_name') and $request->housing_name != '') {
                $search = "%" . $request->housing_name . "%";
                $query->where('housing_name', 'like', $search);
            }
            //户主姓名
            if ($request->has('householder') and $request->householder != '') {
                $search = "%" . $request->householder . "%";
                $query->where('householder', 'like', $search);
            }
        };

        $allBlock=HousingResource::select('block_name')->distinct('block_name')->get();
        $allName=HousingResource::select('housing_name')->distinct('housing_name')->get();
        $allHousing=HousingResource::where($where)->orderBy('created_time', 'desc')->paginate(10);
        return view('admin.housingresource.housingindex',compact('allHousing','allBlock','allName'));
    }
    //新建房源
    public function createHousing(Request $request){
        return view('admin.housingresource.createHousing');
    }
    //保存房源
    public function saveHousing(Request $request){
        return HousingResource::newHousing($request);
    }
    //修改房源
    public function editHousing(Request $request){
        $arr=explode('=',$request->housing_id);
        $housingId=$arr['1'];
        $editHousing=HousingResource::where('id',$housingId)->first();
        return view('admin.housingresource.editHousing',compact('editHousing'));
    }
    //修改房源
    public function updateHousing(Request $request){
        return HousingResource::updateHousing($request);
    }
    /*
     * 分配房源
     * */
    public function sendHousing(Request $request){
       $sendHousing=HousingResource::where('id',$request->housingId)->update(['status'=>1]);
       if($sendHousing){
          $backMsg=['msg'=>200];
          return json_encode($backMsg);
       }
    }
}
